package wineshop.fixture;

import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import wineshop.test.TestDataBeanRemote;

public class Environment {
	public static InitialContext context;

	private String requestedScript;
	private String lastExecutedScript;

	public Environment(String requestedScript) throws NamingException {
		this.requestedScript = requestedScript;
		Properties prop = new Properties();

		// We need to tell the context where and how to look
		prop.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
		prop.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
		prop.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");

		// Should not be necessary for local test (default values), but
		// currently is
		prop.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
		prop.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

		context = new InitialContext(prop);
	}

	public void startScript() {
		try {
			if (requestedScript.equalsIgnoreCase("resetTestData")) {
				TestDataBeanRemote testDataBean = (TestDataBeanRemote) context.lookup("wineshop.test.TestDataBeanRemote");
				testDataBean.cleanData();
				testDataBean.initData();
				lastExecutedScript = requestedScript;
			}
		} catch (Exception e) {
			lastExecutedScript = "";
		}
	}

	public String lastExecutedScript() {
		return lastExecutedScript;
	}
}
